import 'dart:io';

import 'package:get/get.dart';
import 'package:gt_flutter_admin/models/file.dart' as gt_file;
import 'package:gt_flutter_admin/services/api_service.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

class ImagePickerController extends GetxController with StateMixin<File> {
  File? imageFile;

  /// 上传图片
  Future onUploadImageFile() async {
    change(null, status: RxStatus.loading());
    final List<AssetEntity>? assets = await AssetPicker.pickAssets(
      Get.context!,
      pickerConfig: AssetPickerConfig(maxAssets: 1),
    );
    if (assets != null) {
      imageFile = await assets.first.file;
      change(imageFile, status: RxStatus.success());

      // 上传到后端
      gt_file.File file = await uploadFile(imageFile! as gt_file.File);
      return file.url;
    }
  }
}
